//
//  WJY_HomeScondCell.m
//  MooMusic
//
//  Created by wjy on 15/10/21.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "WJY_HomeScondCell.h"
#import "WJY_HotCollectionCell.h"
#import "AFNetworkHandler.h"
#import "WJYValueModel.h"
#import "LTY_FilmAndSongsViewController.h"
// 导入NetWork的头文件
#import "NetWork.h"
@interface WJY_HomeScondCell ()<UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UIImageView *img;
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) UICollectionView *collection;
@property(nonatomic, strong) UILabel *titleOne;
@property(nonatomic, strong) UILabel *titleTwo;
@property(nonatomic, strong) NSMutableArray *hotArr;
@property(nonatomic, strong) NSMutableArray *nowArr;


//@property (nonatomic, strong) NSMutableArray *valArr;


@end
@implementation WJY_HomeScondCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(5, 1, 0, 1);
        layout.itemSize = CGSizeMake(SCREENWIDTH / 3 - 5, SCREENHEIGHT / 4.5);
        layout.minimumInteritemSpacing = 5;
        layout.minimumLineSpacing = 5;
        [layout setScrollDirection:(UICollectionViewScrollDirectionVertical)];
         layout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 30);
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:layout];
        // self.collection.collectionViewLayout = layout;
//        self.collection.backgroundColor = [UIColor colorWithRed:0.937 green:0.973 blue:0.993 alpha:1.000];
        //        UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 100)] autorelease];
        //        img.image = [UIImage imageNamed:@"nav_back_coloer.jpg"];
        //        self.collection.backgroundView = img;
        self.collection.backgroundColor = [UIColor whiteColor];
        
        self.collection.dataSource = self;
        self.collection.delegate = self;
        self.collection.bounces = NO;
        [self.collection registerClass:[WJY_HotCollectionCell class]
            forCellWithReuseIdentifier:@"cell"];
        [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [self.collection registerClass:[WJY_HotCollectionCell class]
            forCellWithReuseIdentifier:@"nowCell"];
        self.titleArr = [NSMutableArray array];
        self.hotArr = [NSMutableArray array];
        self.valueArr = [NSMutableArray array];
        self.valArr = [NSMutableArray array];
        
        
        
        [AFNetworkHandler GETWithAFNByURL:HomeUrl completion:^(id result) {
            self.hotArr = [HomeModel arrayWithModelByArray:result[@"data"][3][@"data"]];
            self.nowArr = [HomeModel arrayWithModelByArray:result[@"data"][4][@"data"]];
            for (NSDictionary *dic in result[@"data"][3][@"data"]) {
                
                [self.valueArr addObject:dic[@"action"][@"value"]];
                
            }
            
            NSLog(@"~~~~~~~%@", self.valueArr);
            for (NSDictionary *dic in result[@"data"][4][@"data"]) {
                [self.valArr addObject:dic[@"action"][@"value"]];
            }
            
            
            for (NSDictionary *dic in result[@"data"][4][@"data"]) {
                [self.valArr addObject:dic[@"action"][@"value"]];
            }
            
            
            // NSLog(@"~~~~~~~%@", self.valueArr);
            
            for (NSDictionary *dic in result[@"data"]) {
                //                [self.titleArr addObject:[dic objectForKey:@"name"]];
                [self.titleArr addObject:dic[@"name"]];
            }
            if ([self.titleArr count] > 0) {
                [self.collection reloadData];
            }
        }];
        [self.contentView addSubview:self.collection];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collection.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
//    NSLog(@"````````````````%@", self.titleArr);

//    self.titleArr = [NSMutableArray array];
//    self.hotArr = [NSMutableArray array];
//    self.valueArr = [NSMutableArray array];
//    self.valArr = [NSMutableArray array];
//    [AFNetworkHandler GETWithAFNByURL:HomeUrl completion:^(id result) {
//        self.hotArr = [HomeModel arrayWithModelByArray:result[@"data"][3][@"data"]];
//        self.nowArr = [HomeModel arrayWithModelByArray:result[@"data"][4][@"data"]];
//        for (NSDictionary *dic in result[@"data"][3][@"data"]) {
//            
//            [self.valueArr addObject:dic[@"action"][@"value"]];
//            
//        }
//
//        NSLog(@"~~~~~~~%@", self.valueArr);
//        for (NSDictionary *dic in result[@"data"][4][@"data"]) {
//            [self.valArr addObject:dic[@"action"][@"value"]];
//        }
//
//        
//        for (NSDictionary *dic in result[@"data"][4][@"data"]) {
//            [self.valArr addObject:dic[@"action"][@"value"]];
//        }
//        
//        
//       // NSLog(@"~~~~~~~%@", self.valueArr);
//
//        for (NSDictionary *dic in result[@"data"]) {
//            //                [self.titleArr addObject:[dic objectForKey:@"name"]];
//            [self.titleArr addObject:dic[@"name"]];
//        }
//        if ([self.titleArr count] > 0) {
//            [self.collection reloadData];
//        }
//    }];
//    NSLog(@"%@", self.titleArr);

//    self.titleArr = [NSMutableArray array];
//    self.hotArr = [NSMutableArray array];
//    self.valueArr = [NSMutableArray array];
//    self.valArr = [NSMutableArray array];
//    
//    [AFNetworkHandler GETWithAFNByURL:HomeUrl completion:^(id result) {
//        self.hotArr = [HomeModel arrayWithModelByArray:result[@"data"][3][@"data"]];
//        self.nowArr = [HomeModel arrayWithModelByArray:result[@"data"][4][@"data"]];
//        for (NSDictionary *dic in result[@"data"][3][@"data"]) {
//            
//            [self.valueArr addObject:dic[@"action"][@"value"]];
//            
//        }
//
//        NSLog(@"~~~~~~~%@", self.valueArr);
//        for (NSDictionary *dic in result[@"data"][4][@"data"]) {
//            [self.valArr addObject:dic[@"action"][@"value"]];
//        }
//
//        
//        for (NSDictionary *dic in result[@"data"][4][@"data"]) {
//            [self.valArr addObject:dic[@"action"][@"value"]];
//        }
//        
//        
//       // NSLog(@"~~~~~~~%@", self.valueArr);
//
//        for (NSDictionary *dic in result[@"data"]) {
//            //                [self.titleArr addObject:[dic objectForKey:@"name"]];
//            [self.titleArr addObject:dic[@"name"]];
//        }
//        if ([self.titleArr count] > 0) {
//            [self.collection reloadData];
//        }
//        
//        
//    }];
//    NSLog(@"%@", self.titleArr);

    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (self.titleArr.count > 0) {
        return 2;
    }
    return 0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.hotArr.count;
    } else if (section == 1) {
        return self.nowArr.count;
    }
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return CGSizeMake(SCREENWIDTH, 30);
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//    self.titleArr = [NSMutableArray array];
//        [AFNetworkHandler GETWithAFNByURL:HomeUrl completion:^(id result) {
//            for (NSDictionary *dic in result[@"data"]) {
////                [self.titleArr addObject:[dic objectForKey:@"name"]];
//                NSLog(@"~~~~~%@", [dic objectForKey:@"name"]);
//            }
//        }];
//    NSLog(@"~~~~~~~~%@",self.titleArr);
        if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            self.titleOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREENWIDTH, 20)];
            if (indexPath.section == 1) {
                self.titleOne.text = self.titleArr[4];

            }
//            self.titleOne.text = self.titleArr[4];
            [header addSubview:self.titleOne];
            return header;
        }
    return nil;
}
//            if (indexPath.section == 0 ){
//                
//                NSLog(@"~~~~%@",self.titleArr);
//                  self.titleOne.text = @"a";
//            } else {
//                UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//                self.titleTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREENWIDTH, 20)];
//                  self.titleTwo.text = @"b";
//                [header addSubview:self.titleTwo];
//                return header;
//            }
////            label.text = @"美妙话语,听了才知道";
//            [header addSubview:label];
//            return header;
//        }
//    } else {
//       return nil;
//    }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
         WJY_HotCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.model = self.hotArr[indexPath.row];
        
        return cell;
    } else {
        WJY_HotCollectionCell *cellN = [collectionView dequeueReusableCellWithReuseIdentifier:@"nowCell" forIndexPath:indexPath];
        cellN.model = self.nowArr[indexPath.item];
        return cellN;
    }
   
//    cell.imgV sd_setImageWithURL:n placeholderImage:<#(UIImage *)#>
//    NSLog(@"%@", self.fistCell.name);
    
    
    
    
    
}
//- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
     self.block (indexPath.item);
    if (indexPath.section == 0) {
        
        NSString *str = self.valueArr[indexPath.item];
        NSString *nameString = [self.hotArr[indexPath.item] valueForKey:@"name"];
        NSString *descString = [self.hotArr[indexPath.item] valueForKey:@"desc"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:str, @"A", nameString, @"B", descString, @"C", nil];
        
        // 发送一个通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNextVC" object:dic userInfo:nil];
        
        
    }
  
    if (indexPath.section == 1) {
        NSString *str = self.valueArr[indexPath.item];
        NSString *nameString = [self.hotArr[indexPath.item] valueForKey:@"name"];
        NSString *descString = [self.hotArr[indexPath.item] valueForKey:@"desc"];
        
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:str, @"A", nameString, @"B", descString, @"C", nil];
        // 发送一个通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNextVC" object:dic userInfo:nil];
    }
    

}
- (void)awakeFromNib {
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
